import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  server: {
    host: '0.0.0.0',
    port: 8006,
    open: true,
    watch: {
      ignored: [
        '**/target/**',
        '**/.idea/**',
        '**/.git/**',
        '../**',            // ignore parent repo (Java project)
      ],
    },
    proxy: {
      '/api/es': {
        // 反代 本地elasticsearch服务
        target: 'http://172.28.153.119:9200',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/es/, ''),
        secure: false,
        // websocket 代理
        // ws: true,
        headers: {
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE,OPTIONS',
          'Access-Control-Allow-Headers': 'Content-Type, Authorization'
        }
      },
      // 外部服务器列表API代理
      '/external-api': {
        target: 'https://register-cahx.shiyuegame.com',
        changeOrigin: true,
        secure: false,  // 暂时禁用SSL验证
        rewrite: (path) => path.replace(/^\/external-api/, ''),
        headers: {
          'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
          'Accept': 'application/json, text/plain, */*',
          'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
          'Accept-Encoding': 'gzip, deflate, br',
          'Cache-Control': 'no-cache',
          'Pragma': 'no-cache'
        },
        configure: (proxy, options) => {
          proxy.on('error', (err, req, res) => {
            console.error('🔴 代理错误:', err.message)
            console.error('🔴 请求URL:', req.url)
          })
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.info('🟡 代理请求:', req.method, req.url)
            console.info('🟡 目标URL:', proxyReq.getHeader('host') + proxyReq.path)
          })
          proxy.on('proxyRes', (proxyRes, req, res) => {
            console.info('🟢 代理响应:', proxyRes.statusCode, req.url)
            console.info('🟢 响应头:', proxyRes.headers)
          })
        }
      },
      '/api/dev': {
        target: 'http://192.168.9.68:8127',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/dev/, '')
      },
      // 只代理特定的API路径到后端服务器
      '^/(api|elog)': {  // 只代理 /api 和 /elog 开头的请求（除了/api/es）
        target: 'http://192.168.68.189:8005',
        changeOrigin: true,
        // ws: true
      }
    }
  },
  base: '/skill/',
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src')
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "@/styles/element/index.scss" as *;`
      }
    }
  },
  optimizeDeps: {
    include: [
      'xe-utils',
      'vxe-table',
      'element-plus',
      'dayjs',
      '@handsontable/vue3',
      'handsontable',
      'vxe-table',
      'fuse.js',
      'marked',
      'segment',
      'vue',
      'vue-router',
      'vue3-json-viewer',
      'vxe-table'
    ]
  }
})
